package com.woniuxy.venueservice.service.impl;


import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.io.FileTypeUtil;
import cn.hutool.core.io.FileUtil;
import cn.hutool.core.lang.Snowflake;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.woniuxy.core.status.CoachState;
import com.woniuxy.core.status.VenueState;
import com.woniuxy.core.util.DateUtil;
import com.woniuxy.core.util.PageInfo;
import com.woniuxy.util.MyOssUtil;
import com.woniuxy.venuedao.mapper.VenueApplyMapper;
import com.woniuxy.venuedao.mapper.VenueInfoMapper;
import com.woniuxy.venuedao.mapper.VenueLoginLogMapper;
import com.woniuxy.venuedao.model.VenueApply;
import com.woniuxy.venuedao.model.VenueInfo;
import com.woniuxy.venuedao.model.VenueLoginLog;
import com.woniuxy.venuemodel.dto.UserLookVenueDto;
import com.woniuxy.venuemodel.dto.VenueLoginDto;
import com.woniuxy.venuemodel.form.UpdateVenueStateForm;
import com.woniuxy.venuemodel.param.*;
import com.woniuxy.venueservice.exception.VenueException;
import com.woniuxy.venueservice.service.VenueInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 场馆表 服务实现类
 * </p>
 *
 * @author 冉杰
 * @since 2024-01-15
 */
@Service
public class VenueInfoServiceImpl extends ServiceImpl<VenueInfoMapper, VenueInfo> implements VenueInfoService {
    @Autowired
    private VenueInfoMapper venueInfoMapper;

    @Autowired
    private VenueApplyMapper venueApplyMapper;
    @Autowired
    private VenueLoginLogMapper venueLoginLogMapper;
    @Resource
    private MyOssUtil myOssUtil;

    @Override
    public void register(VenueRegisterParam param) {
        QueryWrapper<VenueInfo> wrapper = new QueryWrapper<>();
        wrapper.eq("venue_name",param.getVenueName());
        VenueInfo venueInfo = venueInfoMapper.selectOne(wrapper);
        if(!BeanUtil.isEmpty(venueInfo)){
            throw new VenueException(2001,"场馆名称已存在");
        }
        QueryWrapper<VenueInfo> wrapper1 = new QueryWrapper<>();
        wrapper1.eq("venue_account",param.getVenueAccount());
        VenueInfo venueInfo1 = venueInfoMapper.selectOne(wrapper1);
        if(!BeanUtil.isEmpty(venueInfo1)){
            throw new VenueException(2002,"场馆登录账号已存在");
        }
        QueryWrapper<VenueInfo> wrapper2 = new QueryWrapper<>();
        wrapper2.eq("venue_address",param.getVenueAddress());
        VenueInfo venueInfo2 = venueInfoMapper.selectOne(wrapper2);
        if(!BeanUtil.isEmpty(venueInfo2)){
            throw new VenueException(2003,"场馆地址已存在");
        }
        QueryWrapper<VenueInfo> wrapper3 = new QueryWrapper<>();
        wrapper3.eq("venue_phone",param.getVenuePhone());
        VenueInfo venueInfo3 = venueInfoMapper.selectOne(wrapper3);
        if(!BeanUtil.isEmpty(venueInfo3)){
            throw new VenueException(2004,"法人电话已存在");
        }
        //新增场馆注册表信息
        VenueInfo venueInfo4 = BeanUtil.toBean(param, VenueInfo.class);
        venueInfo4.setAuthState(VenueState.IS_NOT_AUTH.getState());
        venueInfo4.setVenueState(VenueState.IS_ENABLE.getState());
        venueInfo4.setCreateTime(DateUtil.dateTimeToString(new Date()));
        venueInfoMapper.insert(venueInfo4);
        //新增场馆申请表信息
        VenueApply venueApply = new VenueApply();
        venueApply.setApplyTime(DateUtil.dateTimeToString(new Date()));
        venueApply.setVenueAddress(venueInfo4.getVenueAddress());
        venueApply.setVenueImg(venueInfo4.getVenueImg());
        venueApply.setVenueLicense(venueInfo4.getVenueLicense());
        venueApply.setVenueName(venueInfo4.getVenueName());
        venueApply.setVenuePhone(venueInfo4.getVenuePhone());
        venueApply.setVenueRemark(venueInfo4.getVenueRemark());
        venueApply.setAuthState(VenueState.IS_NOT_AUTH.getState());
        venueApplyMapper.insert(venueApply);
    }

    @Override
    public VenueLoginDto login(VenueLoginParam param) {
        QueryWrapper<VenueInfo> wrapper = new QueryWrapper<>();
        wrapper.eq("venue_account",param.getVenueAccount());
        VenueInfo venueInfo = venueInfoMapper.selectOne(wrapper);
        if (venueInfo.getAuthState()!=1) throw new VenueException(3010,"场馆未认证无法登录");
        if(BeanUtil.isEmpty(venueInfo)){
            throw new VenueException(3001,"场馆账号不存在");
        }
        if(!venueInfo.getVenuePass().equals(param.getVenuePass())){
            throw new VenueException(3002,"密码错误");
        }
        if(venueInfo.getVenueState()!=VenueState.IS_ENABLE.getState()){
            throw new VenueException(3003,"场馆状态异常");
        }
        VenueLoginDto dto = BeanUtil.toBean(venueInfo, VenueLoginDto.class);
        //添加登录日志
        VenueLoginLog venueLoginLog = new VenueLoginLog();
        venueLoginLog.setVenueId(venueInfo.getVenueId());
        venueLoginLog.setLoginTime(DateUtil.dateTimeToString(new Date()));
        venueLoginLog.setVenueAccount(venueInfo.getVenueAccount());
        venueLoginLog.setLoginAddr(param.getLoginAddr());
        venueLoginLogMapper.insert(venueLoginLog);
        return dto;
    }

    @Override
    public void updateVenueImage(UpdateVenueImageParam param) throws IOException {
        VenueInfo venueInfo = venueInfoMapper.selectById(param.getVenueId());
        if(BeanUtil.isEmpty(venueInfo)){
            throw new VenueException(4001,"场馆不存在");
        }
        if(venueInfo.getVenueState()!= VenueState.IS_ENABLE.getState()){
            throw new VenueException(4002,"场馆账号状态异常");
        }
        if(venueInfo.getAuthState()!=VenueState.IS_AUTH.getState()){
            throw new VenueException(4003,"场馆认证状态异常");
        }
        String fileType = FileTypeUtil.getType(param.getImage().getInputStream());
        if(!fileType.equals("jpg")&&!fileType.equals("png")){
            throw new VenueException(10086,"文件类型错误");
        }
        Snowflake snowflake = new Snowflake(1, 1);
        String prefix = snowflake.nextIdStr();
        String filename = prefix+"."+fileType;
        String url = myOssUtil.uploadFile(param.getImage().getInputStream(), filename, "ranjie11");
        String oldUrl = venueInfo.getVenueImg();
        venueInfo.setVenueImg(url);
        try {
            venueInfoMapper.updateById(venueInfo);
        } catch (Exception e) {
            myOssUtil.deleteFile("ranjie11", FileUtil.getName(oldUrl));
        }
        myOssUtil.deleteFile("ranjie11",FileUtil.getName(oldUrl));
    }

    @Override
    public void refine(RefineParam param) {
        VenueInfo venueInfo = venueInfoMapper.selectById(param.getVenueId());
        if(BeanUtil.isEmpty(venueInfo)){
            throw new VenueException(4001,"场馆不存在");
        }
        if(venueInfo.getVenueState()!= VenueState.IS_ENABLE.getState()){
            throw new VenueException(4002,"场馆账号状态异常");
        }
        if(venueInfo.getAuthState()!=VenueState.IS_AUTH.getState()){
            throw new VenueException(4003,"场馆认证状态异常");
        }
        venueInfo.setVenueRemark(param.getVenueRemark());
        venueInfoMapper.updateById(venueInfo);
    }

    @Override
    public void checkOldPass(String oldPass, Integer venueId) {
        VenueInfo venueInfo = venueInfoMapper.selectById(venueId);
        if(BeanUtil.isEmpty(venueInfo)){
            throw new VenueException(4001,"场馆不存在");
        }
        if(venueInfo.getVenueState()!= VenueState.IS_ENABLE.getState()){
            throw new VenueException(4002,"场馆账号状态异常");
        }
        if(!venueInfo.getVenuePass().equals(oldPass)){
            throw new VenueException(4004,"旧密码输入错误");
        }
    }

    @Override
    public void updatePass(UpdateVenuePassParam param) {
        VenueInfo venueInfo = venueInfoMapper.selectById(param.getVenueId());
        if(BeanUtil.isEmpty(venueInfo)){
            throw new VenueException(4001,"场馆不存在");
        }
        if(venueInfo.getVenueState()!= VenueState.IS_ENABLE.getState()){
            throw new VenueException(4002,"场馆账号状态异常");
        }
        if(venueInfo.getVenuePass().equals(param.getNewPass())){
            throw new VenueException(4005,"新密码与旧密码一致");
        }
        venueInfo.setVenuePass(param.getNewPass());
        venueInfoMapper.updateById(venueInfo);
    }

    @Override
    public void updateVenueAddress(UpdateVenueAddressParam param) {
        VenueInfo venueInfo = venueInfoMapper.selectById(param.getVenueId());
        if(BeanUtil.isEmpty(venueInfo)){
            throw new VenueException(4001,"场馆不存在");
        }
        if(venueInfo.getVenueState()!= VenueState.IS_ENABLE.getState()){
            throw new VenueException(4002,"场馆账号状态异常");
        }
        if(venueInfo.getAuthState()!=VenueState.IS_AUTH.getState()){
            throw new VenueException(4003,"场馆认证状态异常");
        }
        if(venueInfo.getVenueAddress().equals(param.getVenueAddress())){
            throw new VenueException(4006,"新地址与旧地址一致");
        }
        venueInfo.setVenueAddress(param.getVenueAddress());
        venueInfoMapper.updateById(venueInfo);
    }

    @Override
    public void disableVenue(UpdateVenueStateForm form) {
        VenueInfo venueInfo = venueInfoMapper.selectById(form.getVenueId());
        if(BeanUtil.isEmpty(venueInfo)){
            throw new VenueException(4001,"场馆不存在");
        }
        if(venueInfo.getVenueState()==VenueState.IS_DISABLED.getState()){
            throw new VenueException(4007,"场馆账号已禁用");
        }
        venueInfo.setVenueState(VenueState.IS_DISABLED.getState());
        venueInfoMapper.updateById(venueInfo);
    }

    @Override
    public void enableVenue(UpdateVenueStateForm form) {
        VenueInfo venueInfo = venueInfoMapper.selectById(form.getVenueId());
        if(BeanUtil.isEmpty(venueInfo)){
            throw new VenueException(4001,"场馆不存在");
        }
        if(venueInfo.getVenueState()==VenueState.IS_ENABLE.getState()){
            throw new VenueException(4008,"场馆账号已启用");
        }
        venueInfo.setVenueState(VenueState.IS_ENABLE.getState());
        venueInfoMapper.updateById(venueInfo);
    }

    @Override
    public VenueLoginDto byId(Integer venueId) {
        VenueInfo venueInfo = venueInfoMapper.selectById(venueId);
        if(BeanUtil.isEmpty(venueInfo)){
            throw new VenueException(4001,"场馆不存在");
        }
        VenueLoginDto dto = BeanUtil.toBean(venueInfo, VenueLoginDto.class);
        return dto;
    }

    @Override
    public PageInfo<UserLookVenueDto> userLookAllVenue(Integer pageNum, Integer pageSize, String address, String venueName) {
        QueryWrapper<VenueInfo> wrapper = new QueryWrapper<>();
        wrapper.like(!ObjectUtil.isEmpty(address),"venue_address",address);
        wrapper.like(!ObjectUtil.isEmpty(venueName),"venue_name",venueName);
        wrapper.eq("auth_state",1);
        Page<VenueInfo> page = new Page<>(pageNum==null?1:pageNum,pageSize==null?4:pageSize);
        page(page,wrapper);
        PageInfo pageInfo = BeanUtil.toBean(page, PageInfo.class);
        pageInfo.setPages(page.getPages());
        pageInfo.setList(BeanUtil.copyToList(page.getRecords(),UserLookVenueDto.class));
        return pageInfo;
    }

    @Override
    public PageInfo<UserLookVenueDto> adminLookAllVenue(Integer pageNum,Integer pageSize,Integer venueState, Integer authState, String venueName) {
        QueryWrapper<VenueInfo> wrapper = new QueryWrapper<>();
        wrapper.eq(!ObjectUtil.isEmpty(venueState),"venue_state",venueState);
        wrapper.eq(!ObjectUtil.isEmpty(authState),"auth_state",authState);
        wrapper.like(!ObjectUtil.isEmpty(venueName),"venue_name",venueName);
        Page<VenueInfo> page = new Page<>(pageNum==null?1:pageNum,pageSize==null?4:pageSize);
        page(page,wrapper);
        PageInfo pageInfo = BeanUtil.toBean(page, PageInfo.class);
        pageInfo.setPages(page.getPages());
        pageInfo.setList(BeanUtil.copyToList(page.getRecords(),UserLookVenueDto.class));
        return pageInfo;
    }

}
